package com.chenss.dragonser.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chenss.dragonser.R;
import com.chenss.dragonser.app.AppInfo;
import com.chenss.dragonser.app.Constant;
import com.chenss.dragonser.base.BaseActivity;
import com.chenss.dragonser.network.NetworkRequest;
import com.chenss.dragonser.network.RequestCallbcak;
import com.chenss.dragonser.utils.ToastUtil;

import org.json.JSONException;
import org.w3c.dom.Text;

import java.util.logging.Logger;

/**
 * PageName: 选择体现方式
 * Create by chenss at 2018/4/20 2:54
 */
public class PostalWayActivity extends BaseActivity implements RequestCallbcak {
    LinearLayout ll_left;//左边按钮
    TextView tv_page_title;
    LinearLayout ll_alipay, ll_wechat;
    CardView cardView;
    int way = 0;//0,支付宝 1 微信 默认微信支付
    ImageView img_1, img_2;
    Button btn_sub;
    EditText edt_pay_account;//账号
    String amount = "0";//提现金额
    public NetworkRequest networkRequest;//网络请求
    public ArrayMap<String, String> parmMap;//请求参数封装
    TextView tv_amount;

    @Override
    public void initParms(Bundle parms) {
        amount = getIntent().getStringExtra("amount");
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_postal_way;
    }

    @Override
    public void initView(View view) {
        ll_left = findView(R.id.ll_left);//左边按钮
        tv_page_title = findView(R.id.tv_page_title);
        ll_left.setVisibility(View.VISIBLE);
        ll_left.setOnClickListener(this);
        tv_page_title.setText("选择提现方式");


        tv_amount = findView(R.id.tv_amount);
        btn_sub = findView(R.id.btn_sub);
        edt_pay_account = findView(R.id.edt_pay_account);
        cardView = findView(R.id.cardView);
        ll_alipay = findView(R.id.ll_alipay);
        ll_wechat = findView(R.id.ll_wechat);
        img_1 = findView(R.id.img_1);
        img_2 = findView(R.id.img_2);
        ll_wechat.setOnClickListener(this);
        ll_alipay.setOnClickListener(this);
        btn_sub.setOnClickListener(this);

        if (way == 0) {
            //支付宝
            cardView.setVisibility(View.VISIBLE);
            img_1.setVisibility(View.VISIBLE);
            img_2.setVisibility(View.INVISIBLE);
        } else {
            cardView.setVisibility(View.GONE);
            img_1.setVisibility(View.INVISIBLE);
            img_2.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void doBusiness(Context mContext) {
        networkRequest = new NetworkRequest(mContext, this);
        tv_amount.setText(amount);

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ll_left:
                finish();
                break;
            case R.id.btn_sub:
                if (way == 0) {
                    if (TextUtils.isEmpty(edt_pay_account.getText().toString())) {
                        ToastUtil.showShort(this, "请输入提现的支付宝帐号");
                        return;
                    }
                    doAlipay();
                } else {
//                    getWechatKey();
                    ToastUtil.showShort(this, "暂不支持微信提现");
                }


                break;
            case R.id.ll_wechat:
                way = 1;
                cardView.setVisibility(View.GONE);
                img_1.setVisibility(View.INVISIBLE);
                img_2.setVisibility(View.VISIBLE);
                break;
            case R.id.ll_alipay:
                way = 0;
                cardView.setVisibility(View.VISIBLE);
                img_1.setVisibility(View.VISIBLE);
                img_2.setVisibility(View.INVISIBLE);
                break;

        }
    }


    /**
     * 支付宝提现
     */
    private void doAlipay() {
//'userid' => '254', // 用户id
//        'alipay_account' => '17795629580', // 支付宝账号，手机号或邮箱
//        'cash_amount' => '0.1', // 提现金额
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.System, Constant.classes.System_an_ALiPaymentCash);
        parmMap.put("store_id", AppInfo.getUser(this).getStore_id());
        parmMap.put("alipay_account", edt_pay_account.getText().toString());
        parmMap.put("cash_amount", amount);
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_1, true, Constant.classes.System_an_ALiPaymentCash);
    }
    /**
     * 微信密钥
     */
    private void getWechatKey() {
        /* 修改这里 - 下方为接口所要传递的参数 */
//        'userid' => '254', // 用户id
//                'cash_amount' => '1', // 提现金额
//                'wechat_token_key' => 'o-CT1vozN0-Czbh8GrEjkTyzhTqk', // 微信openID
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.System, Constant.classes.System_an_TXunWechat);
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_2, true, Constant.classes.System_an_TXunWechat);
    }
    /**
     * 微信提现
     */
    private void doWechatPay() {
        /* 修改这里 - 下方为接口所要传递的参数 */
//        'userid' => '254', // 用户id
//                'cash_amount' => '1', // 提现金额
//                'wechat_token_key' => 'o-CT1vozN0-Czbh8GrEjkTyzhTqk', // 微信openID
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.System, Constant.classes.System_an_TXunWechatCash);
        parmMap.put("store_id", AppInfo.getUser(this).getStore_id());
        parmMap.put("cash_amount", amount);
        parmMap.put("wechat_token_key", "");
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_3, true, Constant.classes.System_an_TXunWechatCash);
    }
    @Override
    public void onFaild(int orderCode, String result) {
        ToastUtil.showShort(this, result);
    }

    @Override
    public void onSucceed(int orderCode, String result) throws JSONException {
        Logger.getLogger(result);
        if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_1) {
            //提现成功
            ToastUtil.showShort(PostalWayActivity.this, "提现成功！");
            this.finish();
        }else if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_2) {
            //密钥获取成功
//          doWechatPay();
        }else if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_3) {
            //提现成功
            ToastUtil.showShort(PostalWayActivity.this, "提现成功！");
            this.finish();
        }
    }
}
